package com.net.xpay.core.manager.impl;

import com.google.common.base.Preconditions;
import com.net.common.annotation.ReadTransactional;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.AccountLogMapper;
import com.net.xpay.core.domain.AccountLog;
import com.net.xpay.core.enums.AccountBillType;
import com.net.xpay.core.manager.AccountLogManager;
import com.net.xpay.core.model.AccountLogSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.time.LocalDateTime;
import java.util.List;


@Component
@ReadTransactional
public class AccountLogManagerImpl extends AbstractManager<AccountLog> implements AccountLogManager{

    @Autowired
    private AccountLogMapper mapper;

    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(AccountLog obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        return super.save(obj);
    }

    @Override
    protected BasicMapper<Long, AccountLog> getMapper() {
        return mapper;
    }

    @Override
    public List<AccountLog> search(AccountLogSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(AccountLogSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public AccountLog findByRelatedId(Long userId, AccountBillType type, Long relatedId) {
        Preconditions.checkArgument(userId != null && type != null && relatedId != null);
        AccountLog condition = new AccountLog();
        condition.setUserId(userId);
        condition.setType(type);
        condition.setRelatedId(relatedId);
        return getByCondition(condition);
    }

    @Override
    public List<AccountLog> list(AccountLogSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }

    @Override
    public long sumAccountLog(AccountLogSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.sumAccountLog(searchModel);
    }

    @Override
    public long sumLastAfterAmountByTime(LocalDateTime minCreateTime, LocalDateTime maxCreateTime) {
        Preconditions.checkArgument(minCreateTime != null && maxCreateTime != null);
        return mapper.sumLastAfterAmountByTime(minCreateTime, maxCreateTime);
    }

    @Override
    public long sumFirstBeforeAmountByTime(LocalDateTime minCreateTime, LocalDateTime maxCreateTime) {
        Preconditions.checkArgument(minCreateTime != null && maxCreateTime != null);
        return mapper.sumFirstBeforeAmountByTime(minCreateTime, maxCreateTime);
    }
}
